package com.mylg.craw.house.util;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.extern.log4j.Log4j2;

@Log4j2
public class JacksonUtils {
    private static final ObjectMapper mapper = new ObjectMapper();

    public static <T> T readValue(String res, Class<T> tClass) {
        try {
            return mapper.readValue(res, tClass);
        } catch (Exception e) {
            log.warn("反序列化失败,message={},res={},cls={}", e.getMessage(), res, tClass);
        }
        return null;
    }

    public static <T> T readValue(String res, TypeReference<T> typeReference) {
        try {
            return mapper.readValue(res, typeReference);
        } catch (Exception e) {
            log.warn("反序列化失败,message={},res={},cls={}", e.getMessage(), res, typeReference);
        }
        return null;
    }

    public static String writeValueAsString(Object v) {
        try {
            return mapper.writeValueAsString(v);
        } catch (JsonProcessingException e) {
            log.warn("序列化失败,message={}", e.getMessage());
        }
        return null;
    }
}
